package com.foxzzz.deep.first;

import com.foxzzz.base.TreeNode;

/**
 * @author: [wujie]
 * @createTime: [2019/12/11 19:47]
 * @description:
 */
public class Solution110 {


    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        return Math.abs(deep(root.right)-deep(root.left))<2;
    }

    public int deep(TreeNode root) {

        if (root == null) {
            return 0;
        }
        int leftDeep = deep(root.left);
        int rightDeep = deep(root.right);
        int i = Math.abs(leftDeep - rightDeep);
        if (i > 1) {
            return -1;
        }
        return Math.max(leftDeep, rightDeep) + 1;
    }

    public static void main(String[] args) {
        TreeNode treeNode = TreeNode.getTreeNode("[3,9,20,null,null,15,7]");
        TreeNode treeNode3 = TreeNode.getTreeNode("[1,2,2,3,null,null,3,4,null,null,4]");
        TreeNode treeNode1 = TreeNode.getTreeNode("[1,2,2,3,3,null,null,4,4]");
        TreeNode treeNode2 = TreeNode.getTreeNode("[3,9,20,null,null,15,7]");
        Solution110 solution110 = new Solution110();
        System.out.println(solution110.isBalanced(treeNode3));
        System.out.println(solution110.isBalanced(treeNode));
        System.out.println(solution110.isBalanced(treeNode1));
        System.out.println(solution110.isBalanced(treeNode2));



    }
}
